package com.gdpu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpu.bean.*;
import com.gdpu.common.DataGridView;
import com.gdpu.common.ResultObj;
import com.gdpu.service.OutdingdanService;
import com.gdpu.service.GoodsService;
import com.gdpu.service.StockService;
import com.gdpu.vo.OutdingdanVo;
import com.gdpu.vo.OutdingdanVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


@RestController
@RequestMapping("/outdingdan")
public class OutdingdanController {
    @Resource
    private OutdingdanService outdingdanService;
    @Resource
    private GoodsService goodsService;

    //审核员查询客户购买所有数据
    @RequestMapping("loadAllSoutdingdan")
    public DataGridView loadAllSoutdingdan(OutdingdanVo outdingdanVo){
        IPage<Outdingdan> page = new Page<Outdingdan>(outdingdanVo.getPage(),outdingdanVo.getLimit());
        //QueryWrapper可用于查询功能
        QueryWrapper<Outdingdan> queryWrapper = new QueryWrapper<Outdingdan>();
        queryWrapper.eq("type","待审核");
        queryWrapper.eq(null!=outdingdanVo.getSum() && outdingdanVo.getSum()!=0,"sum",outdingdanVo.getSum());
        outdingdanService.page(page,queryWrapper);
        List<Outdingdan> records = page.getRecords();
        for (Outdingdan outdingdan : records) {
            Goods goods = goodsService.getById(outdingdan.getGoodsId());
            if(null != goods){
                outdingdan.setGoodsName(goods.getName());
                outdingdan.setSize(goods.getSize());
                outdingdan.setPackages(goods.getPackages());
                outdingdan.setPrice(goods.getPrice());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //查询审核员的退货订单页面
    @RequestMapping("loadAllSreturndingdan")
    public DataGridView loadAllSreturndingdan(OutdingdanVo outdingdanVo){
        IPage<Outdingdan> page = new Page<Outdingdan>(outdingdanVo.getPage(),outdingdanVo.getLimit());
        //QueryWrapper可用于查询功能
        QueryWrapper<Outdingdan> queryWrapper = new QueryWrapper<Outdingdan>();
        queryWrapper.eq("type","客户退货");
        queryWrapper.eq(null!=outdingdanVo.getSum() && outdingdanVo.getSum()!=0,"sum",outdingdanVo.getSum());
        outdingdanService.page(page,queryWrapper);
        List<Outdingdan> records = page.getRecords();
        for (Outdingdan outdingdan : records) {
            Goods goods = goodsService.getById(outdingdan.getGoodsId());
            if(null != goods){
                outdingdan.setGoodsName(goods.getName());
                outdingdan.setSize(goods.getSize());
                outdingdan.setPackages(goods.getPackages());
                outdingdan.setPrice(goods.getPrice());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }


    @GetMapping("/loadAllOutdingdan")
    public List<OutdingdanVo> voList(){
        return this.outdingdanService.voList();
    }
    //添加
    @RequestMapping("addOutdingdan")
    public ResultObj addOutdingdan(OutdingdanVo outdingdanVo){
        try {
            outdingdanService.save(outdingdanVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    //修改
    @RequestMapping("updateOutdingdan")
    public ResultObj updateOutdingdan(OutdingdanVo outdingdanVo){
        try {
            outdingdanService.updateById(outdingdanVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    @RequestMapping("deleteOutdingdan")
    public ResultObj deleteOutdingdan(Integer id){
        try {
            outdingdanService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    //审核不通过删除订单
    @RequestMapping("deleteSoutdingdan")
    public ResultObj deleteSoutdingdan(Integer id){
        try {
            outdingdanService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    //审核按钮
    public UpdateWrapper<Outdingdan> updateWrapper1(OutdingdanVo outdingdanVo){
        UpdateWrapper<Outdingdan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",outdingdanVo.getId());
        updateWrapper.eq("type","待审核");//type=0:客户购买商品，待审核员审核
        updateWrapper.set("type","审核通过，未付款");//type=1:审核员审核通过，待仓库管理员确认
        return updateWrapper;
    }

    @RequestMapping("updateSoutdingdan")
    public ResultObj updateSoutdingdan(OutdingdanVo outdingdanVo){
        try {
            UpdateWrapper<Outdingdan> updateWrapper =updateWrapper1(outdingdanVo);
            outdingdanService.update(outdingdanVo,updateWrapper);
            return ResultObj.Examine_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.Examine_ERROR;
        }
    }

    //仓库管理员确定出库按钮
    public UpdateWrapper<Outdingdan> updateWrapper2(OutdingdanVo outdingdanVo){
        UpdateWrapper<Outdingdan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",outdingdanVo.getId());
        updateWrapper.eq("type","审核通过，未付款");
        updateWrapper.set("type","已付款，待出库");//type=2:仓库管理员确认订单，商品出库
        return updateWrapper;
    }

    @RequestMapping("updateCoutdingdan")
    public ResultObj updateCoutdingdan(OutdingdanVo outdingdanVo){
        try {
            UpdateWrapper<Outdingdan> updateWrapper =updateWrapper2(outdingdanVo);
            outdingdanService.update(outdingdanVo,updateWrapper);
            return ResultObj.Type_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.Type_ERROR;
        }
    }

    //客户退货
    public UpdateWrapper<Outdingdan> updateWrapper3(OutdingdanVo outdingdanVo){
        UpdateWrapper<Outdingdan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",outdingdanVo.getId());
        updateWrapper.eq("type","已付款，待出库");
        updateWrapper.set("type","客户退货");//type=-1:客户退货
        return updateWrapper;
    }

    @RequestMapping("returnOutdingdan")
    public ResultObj returnOutdingdan(OutdingdanVo outdingdanVo){
        try {
            UpdateWrapper<Outdingdan> updateWrapper =updateWrapper3(outdingdanVo);
            outdingdanService.update(outdingdanVo,updateWrapper);
            return ResultObj.Type_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.Type_ERROR;
        }
    }

    //审核员审核退货订单
    public UpdateWrapper<Outdingdan> updateWrapper4(OutdingdanVo outdingdanVo){
        UpdateWrapper<Outdingdan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",outdingdanVo.getId());
        updateWrapper.eq("type","客户退货");
        updateWrapper.set("type","退货审核通过");//type=-2:审核员审核通过，待仓库管理员确认
        return updateWrapper;
    }

    @RequestMapping("updateSreturndingdan")
    public ResultObj updateSreturndingdan(OutdingdanVo outdingdanVo){
        try {
            UpdateWrapper<Outdingdan> updateWrapper =updateWrapper4(outdingdanVo);
            outdingdanService.update(outdingdanVo,updateWrapper);
            return ResultObj.Examine_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.Examine_ERROR;
        }
    }
}

